import 'package:flutter/material.dart';

class NavigatorDemo extends StatelessWidget {
  const NavigatorDemo({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Container(
        child: Navigator(
          initialRoute: '/home-content',
          onPopPage: (Route<dynamic> route, dynamic result) {
            print('----_onPopPage-----');
            return true;
          },
          onGenerateRoute: (RouteSettings settings) {
            switch (settings.name) {
              case '/home-content':
                return MaterialPageRoute(
                    builder: (_) => HomeContent(), settings: settings);
              case '/red':
                return MaterialPageRoute(
                    builder: (_) => RedPage(), settings: settings);
              case '/yellow':
                return MaterialPageRoute(
                    builder: (_) => YellowPage(), settings: settings);
              case '/green':
                return MaterialPageRoute(
                    builder: (_) => GreenPage(), settings: settings);
              default:
                return MaterialPageRoute(
                    builder: (_) => HomeContent(), settings: settings);
            }
          },
        ),
      ),
    );
  }
}

class HomeContent extends StatelessWidget {
  const HomeContent({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              ElevatedButton(
                style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all(Colors.red)),
                onPressed: () {
                  Navigator.pushNamed(context, '/red');
                },
                child: null,
              ),
              ElevatedButton(
                style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all(Colors.yellow)),
                onPressed: () {
                  Navigator.pushNamed(context, '/yellow');
                },
                child: null,
              ),
              ElevatedButton(
                style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all(Colors.green)),
                onPressed: () {
                  Navigator.pushNamed(context, '/green');
                },
                child: null,
              )
            ],
          ),
        ],
      ),
    );
  }
}

class RedPage extends StatelessWidget {
  const RedPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('RedPage'),
      ),
      body: Container(
        color: Colors.red,
      ),
    );
  }
}

class YellowPage extends StatelessWidget {
  const YellowPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('YellowPage'),
      ),
      body: Container(
        color: Colors.yellow,
      ),
    );
  }
}

class GreenPage extends StatelessWidget {
  const GreenPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('GreenPage'),
      ),
      body: Container(
        color: Colors.green,
      ),
    );
  }
}
